package com.huarui.company.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.ewaf.framework.model.PagingSummary;
import com.huarui.company.dao.ICompanyDao;
import com.huarui.company.model.Company;
import com.huarui.company.model.CompanyMarket;

public class CompanyDaoImpl extends BaseDaoImpl implements ICompanyDao {

	@Override
	public boolean isExistCompanyByTaxCode(String taxCode) {

		if (this.getCompanyByTaxCode(taxCode,null) == null) {
			return Boolean.FALSE;
		} else {
			return Boolean.TRUE;
		}

	}

	@Override
	public boolean isExistCompanyByName(String name) {
		if (this.getCompanyByName(name) == null) {
			return Boolean.FALSE;
		} else {
			return Boolean.TRUE;
		}
	}

	@Override
	public boolean isExistCompanyByEmail(String email) {

		if (this.getCompanyByEmail(email) == null) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	@Override
	public boolean isExistCompanyByCreatorId(Integer creatorId) {
		if (this.getCompanyByCreatorId(creatorId) == null) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	@Override
	public boolean isExistCompanyByActivateCode(String activateCode) {
		if (this.getCompanyByActivateCode(activateCode) == null) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	@Override
	public Company getCompanyByName(String name) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByName", name);

	}

	@SuppressWarnings("unchecked")
	@Override
	public Company getCompanyByTaxCode(String taxCode,Integer paid) {
		Map map = new HashMap();
		
		map.put("taxCode", taxCode);
		map.put("paid",paid);
		
		
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByTaxCode", map);
	}

	@Override
	public Company getCompanyByIdWithCreator(Integer id) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByIdWithCreator", id);
	}

	@Override
	public Company getCompanyByCode(String code) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByCode", code);

	}

	@Override
	public Company getCompanyByPYCode(String pyCode) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByPYCode", pyCode);
	}

	@Override
	public Company getCompanyByEmail(String email) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByEmail", email);

	}

	@Override
	public Company getCompanyByCreatorId(Integer creatorId) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByCreatorId", creatorId);

	}

	@Override
	public Company getCompanyByActivateCode(String activateCode) {
		return (Company) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyByActivateCode", activateCode);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getCompanysByPYCodePrefix(
			String PYCodePrefix, PagingSummary summary) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pyCode", PYCodePrefix + "%");
		params.put("start", summary.getStart());
		params.put("pageSize", summary.getLimit());
		return (List<Map<String, Object>>) this.getSqlMapClientTemplate()
				.queryForList(namespace + ".getCompanyByPYCodePrefix", params);
	}

	public int getCompanysCountByPYCodePrefix(String PYCodePrefix) {
		String pyCode = PYCodePrefix + "%";
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getCompanyCountByPYCodePrefix", pyCode);
	}

	@Override
	public boolean isExistCompanyByCode(String code) {
		if (this.getCompanyByCode(code) == null) {
			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> getCompanysByword(Map map) {

		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getCompanysByword", map);
	}

	@Override
	public boolean updateCompany(Company company) {
		return getSqlMapClientTemplate().update(namespace + ".updateCompany",
				company) == 1;
	}

	@Override
	public Company getBySimpleId(Integer id) {
		
		return (Company) getSqlMapClientTemplate().queryForObject(namespace + ".getBySimpleId", id);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<CompanyMarket> getCompanyMarketsByCompId(Integer compId) {
		return this.getSqlMapClientTemplate().queryForList(super.namespace + ".getCompanyMarketsByCompId", compId);
	}

	@Override
	public Integer getCompanyCountByConditions(Map<String, Object> map) {
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getCompanyCountByConditions", map);
		 
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> getCompanyByConditions(Map<String, Object> map) {
		
		return getSqlMapClientTemplate().queryForList(namespace+".getCompanyByConditions", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> getByContactorIdentityCode(String contactorIdentityCode,Integer paid) {
		
		Map map = new HashMap();
		
		map.put("contactorIdentityCode", contactorIdentityCode);
		map.put("paid",paid);
		
		return (List<Company>) this.getSqlMapClientTemplate().queryForList(
				namespace + ".getByContactorIdentityCode", map);
	}
    /**
     * yi
     */
	@Override
	public Company getCompanyByIdForUpdate(Integer id) {
		return (Company) getSqlMapClientTemplate().queryForObject(namespace+".getCompanyForUpdate", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Company> getCompanyLikeTaxCode(String taxCode) {
		
		return (List<Company>) this.getSqlMapClientTemplate().queryForList(
				namespace + ".getCompanyLikeTaxCode", taxCode);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Company> getLikeContactorIdentityCode(
			String contactorIdentityCode) {
		
		return (List<Company>) this.getSqlMapClientTemplate().queryForList(
				namespace + ".getLikeContactorIdentityCode", contactorIdentityCode);
	}

}
